package com.cropdoctor.controller;

import com.cropdoctor.common.Result;
import com.cropdoctor.model.dto.*;
import com.cropdoctor.model.po.NotePo;
import com.cropdoctor.model.po.RemindPo;
import com.cropdoctor.model.po.VegeFruGardenPo;
import com.cropdoctor.model.vo.VegeFruGardenVO;
import com.cropdoctor.service.NoteService;
import com.cropdoctor.service.VegeFruGardenService;
import com.cropdoctor.util.ContextGet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/garden")
@Api(tags = "果蔬花园:针对单个用户模块")
public class VegAndFruGardenController {

    @Autowired
    VegeFruGardenService vegeFruGardenService;

    @Autowired
    NoteService noteService;

    @GetMapping("")
    @ApiOperation("获取用户种植的所有果蔬")
    public Result<List<VegeFruGardenDto>> getAll() {
        //从threadLocal获取
        long userId = ContextGet.getUserId();
        return vegeFruGardenService.getAll(userId);
    }

    @GetMapping("/{idGarden}")
    @ApiOperation("获取用户种植的单个果蔬信息")
    public Result<SingleVegeFruDetailDto> getSingle(@PathVariable int idGarden){
        //从threadLocal获取
        long userId = ContextGet.getUserId();
        return vegeFruGardenService.getSingle(userId,idGarden);
    }

    @PostMapping("/{idTable}/{name}")
    @ApiOperation("从果蔬表中插入果蔬到用户的果蔬花园中")
    public Result<VegeFruGardenDto> addUserVegAndFruGarden(@PathVariable Long idTable,@PathVariable String name) throws IOException {
        return vegeFruGardenService.addUserVegAndFruGarden(idTable,name);
    }

    @DeleteMapping("/{idGarden}")
    @ApiOperation("根据传入的id将用户种植的果蔬个体从果蔬花园中删除")
    public Result<String> deleteById(@PathVariable Long idGarden) {
        return vegeFruGardenService.deleteByGardenId(idGarden);
    }


    //region备忘模块
    @PostMapping("/remind")
    @ApiOperation("更改备忘数据列表")
    public Result<String> changeRemind(@RequestBody RemindPo remindPo){
        return vegeFruGardenService.changeRemindList(remindPo);
    }

    @GetMapping("/remind/{idGarden}")
    @ApiOperation("根据传入的果蔬花园id查询对应单个果蔬的备忘信息")
    public Result<RemindPo> getRemind(@PathVariable("idGarden") Integer idGarden,@RequestParam(value = "date",required = false) String dateStr){
        LocalDate date = LocalDate.parse(dateStr);
        return vegeFruGardenService.getRemind(idGarden,date);
    }
    //endregion


    //region笔记模块
    @GetMapping("/note/{idGarden}")
    @ApiOperation("根据传入的果蔬花园实体id查询对应的笔记信息")
    public Result<List<NotePo>> getNoteById ( @PathVariable("idGarden") int idGarden){
        return noteService.getNoteById(idGarden);
    }
    @PutMapping("/note")
    @ApiOperation("更新单条笔记")
    public Result<String> updateNoteById (@RequestBody NotePo notePo){
        return noteService.updateNoteById(notePo);
    }

    @PostMapping("/note")
    @ApiOperation("用户选择新的图片进行上传")
    public Result<String> insertOrUpdateNoteById (@RequestParam(value = "img",required = false) MultipartFile img,
                                          @RequestParam(value="remark") String remark,
                                          @RequestParam(value="createDate") String createDateStr,
                                          @RequestParam(value="idGarden") String idGarden,
                                          @RequestParam(value = "id",required = false,defaultValue = "") String id){
        NotePo notePo = new NotePo();
        if(!"".equals(id)){
            notePo.setId(Long.valueOf(id));
        }
        notePo.setRemark(remark);
        notePo.setCreateDate(LocalDate.parse(createDateStr));
        notePo.setIdGarden(Long.valueOf(idGarden));
        return noteService.insertOrUpdateNote(notePo,img);
    }

    @DeleteMapping("/note/{id}")
    @ApiOperation("通过笔记的id，删除单条笔记")
    public Result<String> deleteNoteById ( @PathVariable int id){
        return noteService.deleteNote(id);
    }

    @PostMapping("/note/oldImg")
    @ApiOperation("用户并没有选择新的图片进行上传")
    public Result<String> insertOrUpdateNoteWithoutImg (@RequestBody NotePo notePo){
        return noteService.insertOrUpdateNoteWithoutImg(notePo);
    }
    //endregion
}
